/* 接口地址：http://jx.xuzhixiang.top/ap/api/cart-list.php
接口请求方式：get
接口参数：
     id  用户id */
let id = localStorage.getItem("id");
load();
function load() {
  let API = "http://jx.xuzhixiang.top/ap/api/cart-list.php";
  axios.get(API, { params: { id, pagesize: 100, pagenum: 0 } }).then((r) => {
    console.log(r.data.data);
    let obj = r.data.data;
    let str = "";
    obj.forEach((v) => {
      str += `
            <li>
            <input type='checkbox'  onchange='fn1(${
              v.pid
            },this)' class='del-ck'>
                <img src="${v.pimg}" alt="">
                <p>${v.pname}</p>
                <p>$${v.pprice}</p>
                <span>总价 :</span><span class='sp1'>${v.pnum * v.pprice}</span>
                <span>数量 :</span><span class='sp2'>${v.pnum}</span>
                <button onclick='fn(${v.pid},this)'>删除</button>
                <input type='number' value='${
                  v.pnum
                }' class='ipt' onchange='fn3(${v.pid},this)'>
            </li>
      `;
    });
    let cartUl = (document.querySelector(".ulStr").innerHTML = str);
  });
}
function fn(pid, ipt) {
  let API = "http://jx.xuzhixiang.top/ap/api/cart-delete.php";
  axios.get(API, { params: { pid, uid: id, pnum: ipt.value } }).then((r) => {
    load();
  });
}

function fn1(pid, ipt) {
  let ipts = document.querySelectorAll(".del-ck");
  let allipt = document.querySelector(".ck");
  console.log(allipt);
  let arr = [...ipts];
  console.log(arr);
  arr.forEach((v) => {
    console.log(v.innerHTML);
  });
  let arrs = arr.every(function (v) {
    return v.checked === true;
  });
  allipt.checked = arrs;
}
function fn2() {
  let ipts = document.querySelectorAll(".del-ck");
  let allipt = document.querySelector(".ck");
  console.log(allipt);
  ipts.forEach((v) => {
    return (v.checked = allipt.checked);
  });
}
function fn3(pid, ipt) {
  let id = localStorage.getItem("id");
  let API = "http://jx.xuzhixiang.top/ap/api/cart-update-num.php";

  $.get(API, { pid, uid: id, pnum: ipt.value }, function all(res) {
    let meney = 0;
    let meney1 = 0;
    let arr = document.querySelectorAll(".ipt");
    console.log(arr);
    arr.forEach((ipt) => {
      let arr = ipt.parentNode;
      let arrs = arr.querySelector(".sp2").innerHTML * 1;
      let arrs1 = arr.querySelector(".sp1").innerHTML * 1;
      console.log(arrs);
      // meney = meney + arrs;
      meney1 += arrs1;
      // let p1 = (document.querySelector(".pc1").innerHTML =
      //   "总数量:" + meney1);
      let p = (document.querySelector(".pc").innerHTML = meney1);
    });

    load();
  });
}
